
import java.math.BigDecimal;
import java.math.RoundingMode;

public class CompNumber {
	private double x = 0;
	private double y = 0;

	CompNumber(double x, double y) {
		this.x = x;
		this.y = y;
	}

	CompNumber(double x) {
		this.x = x;
	}

	@Override
	public String toString() {
		if (x != x || y != y) {
			return "Error in calculating";
		}

		double rN = new BigDecimal(x).setScale(10, RoundingMode.HALF_UP)
				.floatValue();
		double rM = new BigDecimal(y).setScale(10, RoundingMode.HALF_UP)
				.floatValue();
		String s = "";
		if (Math.abs(rM) < 0.00001)
			s = rN + "";
		else if (Math.abs(rN) < 0.00001)
			s = rM + "i";
		else
			s = rN + "+" + rM + "i";
		return s;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (this == obj) {
			return true;
		}
		if (this.getClass() == obj.getClass()) {
			CompNumber complexNumber = (CompNumber) obj;
			if (complexNumber.x == x && complexNumber.y == y) {
				return true;
			}
		}
		return false;
	}

	public double getReal() {
		return x;
	}

	public double getImg() {
		return y;
	}

	@Override
	public int hashCode() {
		int hash = 1;
		hash = hash * 17 + (int) x;
		hash = hash * 17 + (int) y;
		return hash;
	}

	public CompNumber plus(CompNumber num) {
		double a, b;
		a = num.x + x;
		b = num.y + y;
		return new CompNumber(a, b);
	}

	public CompNumber minus(CompNumber num) {
		double a, b;
		a = num.x - x;
		b = num.y - y;
		return new CompNumber(a, b);
	}

	public CompNumber multiply(CompNumber num) {
		double a, b;
		a = num.x * x - num.y * y;
		b = num.x * y + num.y * x;
		return new CompNumber(a, b);
	}

	public CompNumber division(CompNumber num) {
		double a, b;
		a = (num.x * x + num.y * y) / (num.x * num.x + num.y * num.y);
		b = (y * num.x - x * num.y) / (num.x * num.x + num.y * num.y);
		return new CompNumber(a, b);
	}

	public CompNumber[] root(int deg) {
		double r = Math.sqrt(x * x + y * y);
		double fi = Math.atan2(y, x);
		double rRoot = Math.pow(r, 1. / deg);
		CompNumber[] result = new CompNumber[deg];
		for (int i = 0; i < deg; i++) {
			double angle = (fi + 2 * i * Math.PI) / deg;
			double real = rRoot * Math.cos(angle);
			double img = rRoot * Math.sin(angle);
			result[i] = new CompNumber(real, img);
		}
		return result;
	}
}